Mapas

Sumário

  • Dados espaciais

  • Leaflet

  • Googleways

Dados espaciais:

  • Pacotes
  • GEOBR
  • Shapefile
  • geoJSON

Dados espaciais:

  • Pacotes
  • GEOBR
  • Shapefile
  • geoJSON

Geobr

Geobr

Geobr

É um pacote para download de conjuntos de dados espaciais oficiais do Brasil. O pacote inclui uma ampla gama de dados geoespaciais em formato de geopacote (como shapefiles).

Exemplo

# Ler município específico em um determinado ano
mun <- geobr::read_municipality(code_muni = 5300108, year = 2017)
# 5300108 = Brasília
# Ler todos os municípios de um estado específico em um determinado ano
mun <- geobr::read_municipality(code_muni = 33, year = 2010) # ou
mun <- geobr::read_municipality(code_muni = "RJ", year = 2010)

# Ler todos os municípios do país em um determinado ano
mun <- geobr::read_municipality(code_muni = "all", year = 2018)

IBGE

Funções

Exemplos

Estados Brasileiros

estados <- geobr::read_state(
  year = 2019, 
  showProgress = FALSE
)
# Remover os eixos do gráfico
sem_eixos <- ggplot2::theme(axis.title=element_blank(),
                 axis.text=element_blank(),
                 axis.ticks=element_blank())

# Plotar todos os estados brasileiros
g1 <- ggplot() +
  geom_sf(data=estados, fill="#2D3E50", color="#FEBF57", size=.15, show.legend = FALSE) +
  labs(subtitle="Estados", size=8) +
  theme_minimal() +
  sem_eixos

Dados espaciais:

  • Pacotes
  • GEOBR
  • Shapefile
  • geoJSON

Shapefile

O que é um Shapefile?

  • Formato popular para dados geoespaciais.
  • Armazena informações geométricas e atributos em múltiplos arquivos.
  • Normalmente composto por três arquivos principais: .shp, .shx, .dbf.

Como encontrar?

  • Sites de instituições geográficas/governo
  • Sites de banco de dados
  • Natural Earth (rnaturalearth)

Aplicação no R

  • Pacotes: Simple Features (sf) e ggplot2
  • Geometria: geom_sf

Funções principais

  • Ler dados espaciais: read_sf, st_read, etc
  • Operações geométricas: st_disjoint, st_intersects, etc
  • Visualização: geom_sf

Cheat Sheet

Aplicação

Banco de dados

  • Natural Earth
  • https://www.naturalearthdata.com/downloads/50m-cultural-vectors/
  • Contém várias informações referente aos países do mundo, incluindo a geometria.

Lendo o banco de dados

my_sf<- read_sf("shapefile/ne_50m_admin_0_countries/ne_50m_admin_0_countries.shp")

my_sf %>%
  select(SOVEREIGNT, POSTAL, CONTINENT, NAME_PT, ISO_A3,geometry) %>%
  head()
Simple feature collection with 6 features and 5 fields
Geometry type: MULTIPOLYGON
Dimension:     XY
Bounding box:  xmin: -73.36621 ymin: -22.40205 xmax: 109.4449 ymax: 41.9062
Geodetic CRS:  WGS 84
# A tibble: 6 × 6
  SOVEREIGNT POSTAL CONTINENT     NAME_PT   ISO_A3                      geometry
  <chr>      <chr>  <chr>         <chr>     <chr>             <MULTIPOLYGON [°]>
1 Zimbabwe   ZW     Africa        Zimbábue  ZWE    (((31.28789 -22.40205, 31.19…
2 Zambia     ZM     Africa        Zâmbia    ZMB    (((30.39609 -15.64307, 30.25…
3 Yemen      YE     Asia          Iémen     YEM    (((53.08564 16.64839, 52.581…
4 Vietnam    VN     Asia          Vietname  VNM    (((104.064 10.39082, 104.083…
5 Venezuela  VE     South America Venezuela VEN    (((-60.82119 9.138379, -60.9…
6 Vatican    V      Europe        Vaticano  VAT    (((12.43916 41.89839, 12.430…
ggplot(my_sf) +
  geom_sf(fill = "#69b3a2", color = "white")+
  theme_minimal() 
library(rnaturalearth)
my_sf |>
  filter(CONTINENT == "South America") |>
  ggplot() +
  geom_sf(fill = "lightblue", color = "black") +
  geom_sf_text(aes(label = NAME_PT), size = 2)+
  theme_minimal()
poluicao<- read_csv("shapefile/GCB2022v27_MtCO2_flat.csv") |>
  filter(Year > 2000) 

mapa_poluicao <- left_join(my_sf |>
                             select(ISO_A3, NAME_PT, geometry),
                           poluicao, by = c("ISO_A3" = "ISO 3166-1 alpha-3"))


mapa<- ggplot(data = mapa_poluicao) +
  geom_sf(aes(fill = Total)) +  
  scale_fill_viridis_c(option = "turbo") +  
  labs(title = "Quantidade de Poluição por País") +
  theme_minimal()

Referências

  • https://ipeagit.github.io/geobr/#r-reading-the-data-as-an-sf-object